var pomelo = require('pomelo');
var utils = require('../util/utils');
var playerDao = module.exports;
//创建用户
playerDao.createPlayer = function(nickName,loginTime ,cb)
{
    var  score = 0;
    var  coinNum = 0 ;
    var  diamondNum = 0 ;
    var  loginTimes = 1 ;
    var  winNum = 0 ;
    var sql = 'insert into player(nickName,score,coinNum,diamondNum,loginTime,loginTimes,winNum) values (?,?,?,?,?,?,?)';
    var args = [nickName,score,coinNum,diamondNum,loginTime,loginTimes,winNum];
    var dbclient = pomelo.app.get('dbclient');
    dbclient.insert(sql,args ,function(err,res){
        if(err){
            console.log('create player for playerDao faild!');
            console.log(err);
            //utils.invokeCallback(cb, null,
        }else{
            console.log('create player ok!');
            // cb(null,res);
            utils.invokeCallback(cb, null,res);
        }

    });

};
//查询用户信息
playerDao.getPlayerInfo = function(playerId ,cb){
    var sql = 'select * from player where playerId = ?';
    var args=[playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao getUserInfo fail!!!!!!');
            console.log(err);
        }else{
            if(!!res && res.length===1){
                console.log('playerDao getUserInfo start=======>');
                var rs = res[0];
                //  cb(null,rs);
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('playerDao getUserInfo2 error ======>');
            }
        }
    });
};

//查询积分榜
playerDao.getScoreList = function(target,cb){
    var sql = 'select playerId,score,nickName from player order by score desc limit 0,?';
    var args=[target];
    console.log('args=',args);
    var dbclient = pomelo.app.get('dbclient');
    console.log('dbclinet:',dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao getScoreList fail');
            console.log(err);
        }else{
            if(!!res &&res.length===1){
                console.log('playerDao getScoreList start======>');
                var rs = res[0];
                console.log('rs===========>',rs);
                utils.invokeCallback(cb,null,rs);
            }else{
                //积分榜
                console.log('playerDao getScoreList=',res);
                utils.invokeCallback(cb,null,res);
            }
        }
    });
};

//查询富豪榜
playerDao.getRichList = function(target,cb){
    var sql = 'select playerId,coinNum,nickName from player order by coinNum desc limit 0,?';
    var args=[target];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao getRichList fail');
            console.log(err);
        }else{
            //返回富豪榜
            console.log('playerDao getCoinList=',res);
            utils.invokeCallback(cb,null,res);
        }
    });
};
//更改昵称
playerDao.updateNickName = function(nickName,playerId,cb){
    var sql = 'update player set nickName = ? where playerId =?';
    var args=[nikcName,playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao updateNickName fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('playerDao updateNickName fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};
//根据昵称在数据库是否存在
playerDao.getPlayerByNickName = function(nickName,cb){
    var sql = 'select playerId from player where nickName =?';
    var args=[nickName];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao getPlayerByNickNamefail');
            console.log(err);
        }else{
            if(!!res &&res.length===1){
                utils.invokeCallback(cb,null,true);
            }else{
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};
//角色信息的管理
playerDao.getActorInfo = function(playerId ,cb){
    var sql = 'select * from player_actor where playerId =?';
    var args=[playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,actorList){
        if(err){
            console.log('playerDao getActorInfo fail');
            console.log(err);
        }else{
            var actorArry =[];
            for(var i in actorList){
                var pieceData = [actorList[i].pLevel1,actorList[i].pLevel2,actorList[i].pLevel3,actorList[i].pLevel4];
                var skillData = [actorList[i].sLevel1,actorList[i].sLevel2,actorList[i].sLevel3];
                var attrData =  [actorList[i].aLevel1,actorList[i].aLevel2,actorList[i].aLevel3];
                var actorJson ={pieces : JSON.parse(JSON.stringify(pieceData)) ,skills:JSON.parse(JSON.stringify(skillData)) ,attrs:JSON.parse(JSON.stringify(attrData)),active:actorList[i].active};
                actorArry.push(actorJson);
            }
            utils.invokeCallback(cb, null,JSON.parse(JSON.stringify(actorArry)));
        }
    });

};

//创建角色(角色管理插入)
playerDao.createActor = function(playerId,actorId,activity,cb){
    var sql = 'insert into player_actor values (?,?,1,1,1,1,1,1,1,1,1,1,?)';
    var args = [playerId,actorId,activity];
    var dbclient = pomelo.app.get('dbclient');
    dbclient.insert(sql,args ,function(err,res){
        if(err){
            console.log('creatActor for playerDao faild!');
            console.log(err);
        }else{
            console.log('createActor ok!');
            cb();
        }

    });
};
//通过playerId 、actorId获取当前用户的角色信息信息
playerDao.getPlayerActorInfo= function(playerId ,actorId,cb){
    var sql ='select a.* ,b.* from player a join player_actor b on a.playerId = b.playerId where a.playerId=? and b.actorId =?';
    var args = [playerId,actorId];
    console.log('args===============>',args);
    var dbclient = pomelo.app.get('dbclient');
    dbclient.query(sql,args,function(err ,res){
        if(err){
            console.log('creatActor for playerDao faild!');
            console.log(err);
        }else{
            if(!!res && res.length===1){
                console.log('playerDao getPlayerActorInfo start=======>');
                var rs = res[0];
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('playerDao getPlayerActorInfo fail!!!!');
            }
        }
    });
};

//更改用户信息(用户的胜场数)
playerDao.updatePlayer = function(args,cb){
    var sql = 'update player set score=?,winNum  = ? where playerId =?';
    var info =[args.score,args.winNum,args.playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('playerDao updatePlayer fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('playerDao updatePlayer fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};
//更改用户coinNum、diamondNum
playerDao.updatePlayerInfo = function(args,cb){
    var sql = 'update player set coinNum  = ? ,diamondNum = ? where playerId =?';
    var info =[args.coinNum,args.diamondNum,args.playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('playerDao updatePlayerInfo fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('playerDao updatePlayerInfo fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};

//更改用户信息
playerDao.updatePlayerInfo2 = function(args,cb){
    var sql = 'update player set loginTime  = ? ,loginTimes = ? where playerId =?';
    var info =[args.loginTime,args.loginTimes,args.playerId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,info,function(err,res){
        if(err){
            console.log('playerDao updatePlayerInfo2 fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                utils.invokeCallback(cb ,null,true);
            }else{
                console.log('playerDao updatePlayerInfo2 fail');
                utils.invokeCallback(cb,null,false);
            }
        }
    });
};

//查询表中player_actor中的角色激活状况
playerDao.getActorActive = function(playerId,cb){
    var sql = 'select count(*) as total from where playerId= ? and active=1';
    var args = [playerId];
    var dbclient  = pomelo.app.get('dbclient');
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerdao getActorActive fail');
            console.log('err=============>',err);
        }else{
            if(!!res && res.length===1){
                console.log('playerDao getActorActive start=======>');
                var rs = res[0];
                utils.invokeCallback(cb, null,rs);
            }else{
                console.log('playerDao getActorActive error ======>');
            }
        }
    });
};

//激活用户
playerDao.updatePlayerActor = function(player,cb){
    var sql = 'update player_actor set active  = 1 where playerId = ? and actorId = ?';
    var args =[player.playerId,player.actorId];
    var dbclient = pomelo.app.get('dbclient');
    console.log(dbclient);
    dbclient.query(sql,args,function(err,res){
        if(err){
            console.log('playerDao updatePlayerActor fail');
            console.log(err);
        }else{
            if(!!res && res.affectedRows >0){
                console.log('playerDao updatePlayerActor ok!!!!!');
                cb();
            }else{
                console.log('playerDao updateUserActor fail');
                cb();
            }
        }
    });
};



